<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/9/3
 * Time: 下午4:41
 */

class Wenjuan_excel_model extends Base_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function getExportData($wenjuanid)
    {
        //获取问卷类型
        $wjdata = $this->db->select('w.wenjuan as title,g.id as gid,g.type as type,g.restype as restype')->from('wenjuan as w')->join('wenjuan_group as g','g.id=w.wenjuangroupid','left')->where('w.id',$wenjuanid)->get()->row_array();
        $wjtype = (int)$wjdata['type'];
        $restype = (int)$wjdata['restype'];
        $gid = (int)$wjdata['gid'];
        $title = $wjdata['title'];

        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();
        //获取填写问卷用户ID
        $uids = $redis->sMembers('wenjuan.'.$wenjuanid.'.complished.uid');

        if ($restype === 1){
            $excel = '';
        }elseif ($restype === 2){
            //Up少年问卷
            $excel = $this->getUpChildExcelData($wenjuanid,$title,$gid,$uids,$redis);
        }elseif ($restype === 3){
            $excel = $this->getWorriesExcelData($wenjuanid,$title,$gid,$uids,$redis);
        }elseif ($restype === 4){
            $excel = $this->getHappinessExcelData($wenjuanid,$title,$gid,$uids,$redis);
        }
        return $excel;
    }

    public function getUpChildExcelData($wenjuanid,$title,$gid,$uids,$redis)
    {
        $this->load->model('wenjuan_model');
        $qids = $this->db->select('id')->from('wenjuan_question')->where('wenjuangroupid ='.$gid)
            ->order_by('sort','ASC')
            ->get()->result_array();

        $excel = array();
        //EXCEL表头
        $excel[] = array('姓名','PMB','PMG','PVB','PVG','HOB','PSB','PSG','B类总分','G类总分','G-B');
        foreach ($uids as $uid){
            //读取答案
            $redis->multi();
            foreach ($qids as $qid){
                $redis->get('wenjuan.'.$wenjuanid.'u'.$uid.'.q'.$qid['id']);
            }
            $ansres = $redis->exec();
            $ans = array();
            foreach ($ansres as $a){
                $ansrow = array();
                $ansrow['key']=$a;
                $ans[] = $ansrow;
            }
            //计算结果
            $data =$this->wenjuan_model->getUpChildResults($gid,$redis,$wenjuanid,$uid,$ans);
            $rowdata = array();
            //获取用户名
            $username = $redis->get('wenjuan.username.'.$wenjuanid.'.'.$uid);
            $rowdata[] = $username;
            foreach ($data['bcells'] as $row){
                //EXCEL数据
                $rowdata[] = $row['rowcells'][0];
            }
            $excel[] = $rowdata;
        }
        return array('name'=>$title.'测试结果.xlsx','body'=>$excel);
    }

    //测测你有多焦虑
    public function getWorriesExcelData($wenjuanid,$title,$gid,$uids,$redis)
    {
        $excel = array();
        //获取问题选项
        $example = $this->db->select('id')->from('wenjuan_question')->where('wenjuangroupid ='.$gid)
            ->limit(1,0)->get()->row_array();
        $items = $this->db->select('key')->from('wenjuan_item')->where('qid',$example['id'])->order_by('sort','ASC')->get()->result_array();

        //EXCEL表头
        $excel[] = array('姓名','总得分');
        foreach ($uids as $uid){
            //计算结果
            $this->load->model('wenjuan_model');
            $data = $this->wenjuan_model->getWorriesResults($gid,$redis,$wenjuanid,$uid,$items);
            $rowdata = array();
            //获取用户名
            $username = $redis->get('wenjuan.username.'.$wenjuanid.'.'.$uid);
            $rowdata[] = $username;
            foreach ($data['bcells'] as $row){
                //EXCEL数据
                $rowdata[] = $row['rowcells'][0];
            }
            $excel[] = $rowdata;
        }
        return array('name'=>$title.'测试结果.xlsx','body'=>$excel);
    }

    //你的幸福是什么
    public function getHappinessExcelData($wenjuanid,$title,$gid,$uids,$redis)
    {
        $excel = array();
        //获取问题选项
        $example = $this->db->select('id')->from('wenjuan_question')->where('wenjuangroupid ='.$gid)
            ->limit(1,0)->get()->row_array();
        $items = $this->db->select('key')->from('wenjuan_item')->where('qid',$example['id'])->order_by('sort','ASC')->get()->result_array();

        //EXCEL表头
        $excel[] = array('姓名','快乐取向','投入去向','意义取向','胜利取向');
        foreach ($uids as $uid){
            //计算结果
            $this->load->model('wenjuan_model');
            $data = $this->wenjuan_model->getHappinessResults($gid,$redis,$wenjuanid,$uid,$items);
            $rowdata = array();
            //获取用户名
            $username = $redis->get('wenjuan.username.'.$wenjuanid.'.'.$uid);
            $rowdata[] = $username;
            foreach ($data['bcells'] as $row){
                //EXCEL数据
                $rowdata[] = $row['rowcells'][0];
            }
            $excel[] = $rowdata;
        }
        return array('name'=>$title.'测试结果.xlsx','body'=>$excel);
    }

    public function getChoiceData($wenjuanid)
    {
        //获取问卷类型
        $wjdata = $this->db->select('w.wenjuan as title,g.id as gid,g.type as type,g.restype as restype')->from('wenjuan as w')->join('wenjuan_group as g','g.id=w.wenjuangroupid','left')->where('w.id',$wenjuanid)->get()->row_array();
        $restype = (int)$wjdata['restype'];
        $gid = (int)$wjdata['gid'];
        $title = $wjdata['title'];
        //获取问卷问题
        $qarr = $this->db->select('id,question,sort')->from('wenjuan_question')->where('wenjuangroupid',$gid)->order_by('sort','ASC')->get()->result_array();

        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();

        $excel = $this->getChoiceExcelData($wenjuanid,$title,$qarr,$redis);

        return $excel;
    }

    public function getChoiceExcelData($wenjuanid,$title,$qarr,$redis)
    {
        $excel = array();
        $excel[] = array('序号','问题','选项','选择人数','选择人');
        foreach ($qarr as $q){
            //获得问题选项
            $items = $this->db->select('key,item')->from('wenjuan_item')->where('qid',$q['id'])->get()->result_array();
            //获得选择该选项的用户
            foreach ($items as $k=>$item){
                $excelrow = array();
                $excelrow[] = $k===0?$q['sort']:'';
                $excelrow[] = $k===0?$q['question']:'';
                $excelrow[] = $item['item'];
                $choosers = $redis->sMembers('wenjuan.'.$wenjuanid.'.q'.$q['id'].'.'.$item['key']);
                //获取用户名
                $usernamestr = '';
                //获取用户数
                $usercount = 0;
                foreach ($choosers as $c){
                    $iscomplished = $redis->getBit('wenjuan.'.$wenjuanid.'.complished',$c);
                    if ($iscomplished===1){
                        $usernamestr .= $redis->get('wenjuan.username.'.$wenjuanid.'.'.$c);
                        $usernamestr .= ',';
                        $usercount++;
                    }
                }
                $excelrow[] = $usercount;
                $excelrow[] = $usernamestr;
                $excel[] = $excelrow;
            }
        }
        return array('name'=>$title.'选项统计结果.xlsx','body'=>$excel);
    }
}